package com.hnucm.cooklife.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.widget.Toolbar;

import com.blankj.utilcode.util.ToastUtils;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.base.BaseActivity;
import com.hnucm.cooklife.base.Utils;
import com.hnucm.cooklife.base.utils.Constant;
import com.hnucm.cooklife.bean.me.LoginData;
import com.hnucm.cooklife.contract.login.Contract;
import com.hnucm.cooklife.presenter.me.LoginPresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<Contract.ILoginView, LoginPresenter> implements Contract.ILoginView {
    private String mUserNameText;
    private String mPassWordText;

    @BindView(R.id.username)
    EditText mUsername;

    @BindView(R.id.password)
    EditText mPassword;

    @BindView(R.id.login)
    Button mLogin;

    @BindView(R.id.register)
    Button mRegister;
    @BindView(R.id.login_toolbar)
    Toolbar mToolbar;
    private Context mContext;

    private String mRegisterName;
    private String mRegisterPassword;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mContext = getApplicationContext();
        try {
            mRegisterName = getIntent().getExtras().getString(Constant.EXTRA_KEY_USERNAME);
            mRegisterPassword = getIntent().getExtras().getString(Constant.EXTRA_VALUE_PASSWORD);
            mUsername.setText(mRegisterName);
            mPassword.setText(mRegisterPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        initToolbar();
    }
    private void initToolbar() {
        getWindow().setStatusBarColor(Utils.getColor(mContext));
        mToolbar.setBackgroundColor(Utils.getColor(mContext));
        mToolbar.setTitle(R.string.login);
        mToolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
    }

    @OnClick(R.id.login)
    public void onClick(){

        mUserNameText = mUsername.getText().toString();
        mPassWordText = mPassword.getText().toString();
        mPresenter.login(mUserNameText,mPassWordText);
        this.finish();
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void onLogin(LoginData loginData) {
        if(loginData != null){
            if(loginData.getCode() == 0){
                finish();
            } else {
                ToastUtils.showShort(loginData.getMsg());
            }
        }else {
            ToastUtils.showShort("meiyoushuj");
        }
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoadingFailed() {

    }

    @Override
    public void onLoadSuccess() {

    }

    @OnClick(R.id.login)
    public void login(){
        if(TextUtils.isEmpty(mUsername.getText()) || TextUtils.isEmpty(mPassword.getText())){
            ToastUtils.showShort("tianhaolema");
            return;
        }
        mUserNameText = mUsername.getText().toString();
        mPassWordText = mPassword.getText().toString();
        mPresenter.login(mUserNameText,mPassWordText);
    }
}
